package com.channgam.dbmlib.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class MySQLJDBC implements iDataAdapter {

    private Connection conn;
    private PreparedStatement pstmt;
    private ResultSet rs;
    private String host;
    private String username;
    private String password;
    private String dbName;

    public MySQLJDBC(String host, String username, String password, String dbName) {
        this.host = host;
        this.username = username;
        this.password = password;
        this.dbName = dbName;
    }

    public void connect() throws Exception {
        Class.forName("com.mysql.jdbc.Driver");

        conn =
                (Connection) DriverManager.getConnection("jdbc:mysql://" + host + "/"
                + dbName + "?characterEncoding=UTF-8", username, password);
    }

    public void query(String sql) throws Exception {
        pstmt = (PreparedStatement) conn.prepareStatement(sql);
        rs = pstmt.executeQuery();
    }

    public void statement(String sql) throws Exception {
       pstmt = (PreparedStatement) conn.prepareStatement(sql);
    }

    public int execute() throws Exception {
        return pstmt.executeUpdate();
    }

    public boolean resultNext() throws Exception {
        return rs.next();
    }

    public ResultSet result() {
        return rs;
    }    

    public void close() throws Exception{
        if(rs != null)
            rs.close();
        if(pstmt != null)
            pstmt.close();
        if(conn != null)
            conn.close();
    }

    public Connection getConn() {
        return conn;
    }

    public void setConn(Connection conn) {
        this.conn = conn;
    }

    public String getDbName() {
        return dbName;
    }

    public void setDbName(String dbName) {
        this.dbName = dbName;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public PreparedStatement getPstmt() {
        return pstmt;
    }

    public void setPstmt(PreparedStatement pstmt) {
        this.pstmt = pstmt;
    }

    public ResultSet getRs() {
        return rs;
    }

    public void setRs(ResultSet rs) {
        this.rs = rs;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }




}
